package com.codestudy.service.impl;

import cn.undraw.util.ConvertUtils;
import cn.undraw.util.result.R;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.codestudy.common.LoginUtil;
import com.codestudy.dao.CommentDao;
import com.codestudy.dao.CommentLikeDao;
import com.codestudy.dao.UserDao;
import com.codestudy.dto.CommentDTO;
import com.codestudy.entity.Comment;
import com.codestudy.entity.CommentLike;
import com.codestudy.entity.User;
import com.codestudy.service.CommentService;
import com.codestudy.service.UserService;
import com.codestudy.vo.CommentVO;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

@Service
public class CommentServiceImpl extends ServiceImpl<CommentDao, Comment> implements CommentService {
    @Resource
    CommentLikeDao commentLikeDao;
    @Resource
    private CommentDao commentDao;
    @Resource
    private UserService userService;
    @Resource
    private UserDao userDao;

    @Override
    public R _save(CommentDTO commentDTO) {
        Comment comment = new Comment();
        comment.setUid(LoginUtil.getCurrentId());
        comment.setCreateTime(LocalDateTime.now());
        ConvertUtils.copy(commentDTO, comment);
        if (StringUtils.isNotBlank(commentDTO.getContent())) {
            if (commentDao.insertComment(comment) < 1) { //如果没有添加成功即报错
                throw new RuntimeException();
            }
            User user = userService.queryById(comment.getUid());
            comment.setUser(user);
            return R.ok("添加成功！");
        }
        return R.fail("添加失败！请填写评论内容");
    }

    /**
     * 查询评论列表
     */
    @Override
    public List<CommentVO> commentSelect(Integer noteId) {
        List<CommentVO> commentVOList = commentDao.commentSelect(noteId);
        if (CollectionUtils.isEmpty(commentVOList)) {
            return Collections.emptyList(); //return []
        }

        commentVOList.forEach(item -> {
            /* 查询点赞数并插入*/
            Integer likes = item.getId(); //
            CommentLike commentLike = new CommentLike();
            commentLike.setCommentId(likes);

            List<CommentLike> commentLikeList = commentLikeDao.selectBycommentLikeList(commentLike);
            if (!CollectionUtils.isEmpty(commentLikeList)) { //判断评论点赞表是否有数据
                item.setLikes(commentLikeList.size());  //将点赞数赋值
                item.setLikesIf(1); //有数据就赋值1
            }

            /*将用户信息插入user字段显示给前端*/
            Integer uid = item.getUid();
            User user = userDao.queryById(uid);
            item.setUser(user);

        });
        return commentVOList;
    }

    @Override
    public Integer selectUid(Integer id) {
        return commentDao.selectByUid(id);
    }

    @Override
    public Integer selectArticle(Integer id) {
        return commentDao.selectArticle(id);
    }

}
